package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/category")
@Api(tags = "管理端-分类接口")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;


    //根据类型查询分类
    @GetMapping("/list")
    public Result<List<Category>> list(@RequestParam Integer type) {
        log.info("查询分类，类型为{}", type);
        List<Category> list = categoryService.list(type);
        return Result.success(list);
    }

    //修改分类
    @PutMapping
    public Result update(@RequestBody CategoryDTO categoryDTO) {
        log.info("修改分类：{}", categoryDTO);
        categoryService.update(categoryDTO);
        return Result.success();
    }

    //分页条件查询
    @GetMapping("/page")
    public Result<PageResult> page(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageResult pageResult = categoryService.pageQuery(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize(),categoryPageQueryDTO.getName(),categoryPageQueryDTO.getType());
        return Result.success(pageResult);
    }

    //添加分类
    @PostMapping
    public Result save(@RequestBody CategoryDTO categoryDTO){
        log.info("新增分类：{}",categoryDTO);
        categoryService.save(categoryDTO);
        return Result.success();
    }

    //删除 分类
    @DeleteMapping
    public Result delete(@RequestParam(value = "id") Long id){
        categoryService.delete(id);
        return Result.success();
    }

    //启用禁用
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable Integer status,@RequestParam Long id){
        log.info("启用禁用：{}",status);
        log.info("id：{}",id);
        categoryService.startOrStop(status,id);
        return Result.success();
    }


}
